package com.ithinks.flow.common.utils;

import com.ithinks.flow.common.utils.protocol.http.URLConnetion.URLConnectionUtils;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @Description
 * @ClassName AddressUtils
 * @Author MR.LI
 * @date 2020.12.04 13:38
 */
public class AddressUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(AddressUtils.class);

    public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";

    private static boolean addressEnabled = false;

    public static String getRealAddressByIP(String ip){
        String address = "";
        try
        {
            if (addressEnabled) {
                address = URLConnectionUtils.sendPost(IP_URL, "ip=" + ip);
                JSONObject json = JSONObject.fromObject(address);
                JSONObject object = json.getJSONObject("data");
                String region = object.getString("region");
                String city = object.getString("city");
                address = region + " " + city;
            }
        }
        catch (Exception e)
        {
            LOGGER.error("获取地理位置异常:", e.getMessage());
        }
        return address;
    }

}
